package edu.gatech.wms.views;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import edu.gatech.wms.R;
import edu.gatech.wms.controllers.UserManager;

public class RegisterActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		findViewById(R.id.submit).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				View focusView = null;
				boolean cancel = true;
				EditText firstName = (EditText) findViewById(R.id.register_fn);
				EditText lastName = (EditText) findViewById(R.id.register_ln);
				EditText email = (EditText) findViewById(R.id.register_email);
				EditText password = (EditText) findViewById(R.id.register_password);
				EditText confirmPassword = (EditText) findViewById(R.id.register_password_confirm);

				String _firstName = firstName.getText().toString();
				String _lastName = lastName.getText().toString();
				String _email = email.getText().toString();
				String _password = password.getText().toString();
				String _confirmPassword = confirmPassword.getText().toString();

				if (TextUtils.isEmpty(_firstName))
				{
					firstName.setError(getString(R.string.error_field_required));
					focusView = firstName;
					cancel = false;
				}

				if (TextUtils.isEmpty(_lastName))
				{
					lastName.setError(getString(R.string.error_field_required));
					focusView = lastName;
					cancel = false;
				}

				if (TextUtils.isEmpty(_email))
				{
					email.setError(getString(R.string.error_field_required));
					focusView = email;
					cancel = false;
				} else if (!_email.contains("@"))
				{
					email.setError(getString(R.string.error_invalid_email));
					focusView = email;
					cancel = false;
				}

				if (TextUtils.isEmpty(_password))
				{
					password.setError(getString(R.string.error_field_required));
					focusView = lastName;
					cancel = false;
				}

				if (_confirmPassword.length() < 8)
				{
					confirmPassword
							.setError(getString(R.string.error_field_required));
					focusView = confirmPassword;
					cancel = false;
					Toast.makeText(getBaseContext(), "Your password must be atleast 8 characters", Toast.LENGTH_SHORT).show();
				}
				
				if (_password.length() < 8)
				{
					password.setError(getString(R.string.error_field_required));
					focusView = lastName;
					cancel = false;
					Toast.makeText(getBaseContext(), "Your password must be atleast 8 characters", Toast.LENGTH_SHORT).show();
				}

				if (TextUtils.isEmpty(_confirmPassword))
				{
					confirmPassword
							.setError(getString(R.string.error_field_required));
					focusView = confirmPassword;
					cancel = false;
				}

				if (!_password.equals(_confirmPassword))
				{
					password.setText("");
					confirmPassword.setText("");
					Toast.makeText(getBaseContext(), "Passwords don't match",
							Toast.LENGTH_LONG).show();
					focusView = confirmPassword;
					cancel = false;
				}

				if (cancel)
				{
					UserManager.createUser(_firstName, _lastName, _password, _email,
							false);
					Toast.makeText(getBaseContext(), "Account Created!", Toast.LENGTH_SHORT).show();
					finish();
				} else
				{
					focusView.requestFocus();
				}

			}
		});

		findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

}
